<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <OrgTree class="w-1/4 xl:w-1/5" @select="handleSelect" />
    <BasicTable
      class="w-3/4 xl:w-4/5"
      @register="registerTable"
      :searchInfo="searchInfo"
    >
      <template #toolbar>
        <a-button
          type="primary"
          v-if="hasPermission(['sysUser:add'])"
          @click="handleCreate"
          >新增
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              auth: 'sysUser:page',
              label: '查看',
              onClick: handleWatch.bind(null, record),
            },
            {
              auth: 'sysUser:edit',
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              auth: 'sysUser:enable',
              label: '启用',
              color: 'success',
              ifShow: () => record.status === 2,
              onClick: handleEnable.bind(null, record.id),
            },
            {
              auth: 'sysUser:deactivate',
              label: '停用',
              color: 'error',
              ifShow: () => record.status === 0,
              onClick: handleDisabled.bind(null, record.id),
            },
          ]"
        />
      </template>
    </BasicTable>
    <AccountModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts">
export default defineComponent({
  name: 'SysUser',
});
</script>
<script lang="ts" setup>
import { defineComponent, reactive } from 'vue';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import {
  getSysUserPage,
  enableSysUser,
  deactivateSysUser,
} from '@/api/sys/sysUserManage';
import { PageWrapper } from '@/components/Page';
import { usePermission } from '@/hooks/web/usePermission';
import { useModal } from '@/components/Modal';
import AccountModal from './AccountModal.vue';
import OrgTree from './OrgTree.vue';
import { columns, searchFormSchema } from './account.data';
import { useMessage } from '@/hooks/web/useMessage';
const { hasPermission } = usePermission();
const { createMessage } = useMessage();
const [registerModal, { openModal }] = useModal();
let searchInfo = reactive<Recordable>({});
const [registerTable, { reload }] = useTable({
  title: '账号列表',
  api: getSysUserPage,
  rowKey: 'id',
  columns,
  showIndexColumn: false,
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
    autoSubmitOnEnter: true,
    baseColProps: {
      span: 8,
    },
  },
  useSearchForm: true,
  bordered: true,
  handleSearchInfoFn(info) {
    console.log('handleSearchInfoFn', info);
    return info;
  },
  actionColumn: {
    width: 180,
    title: '操作',
    dataIndex: 'action',
    slots: { customRender: 'action' },
  },
});
function handleSelect(orgId = '') {
  searchInfo.orgId = orgId;
  reload();
}
function handleCreate() {
  openModal(true, {
    type: 'create',
  });
}
async function handleDisabled(id) {
  await deactivateSysUser(id);
  reload();
  createMessage.success('操作成功');
}
async function handleEnable(id) {
  await enableSysUser(id);
  reload();
  createMessage.success('操作成功');
}
function handleWatch(record: Recordable) {
  openModal(true, {
    record,
    type: 'watch',
  });
}

function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    type: 'edit',
  });
}

function handleSuccess() {
  reload();
}
</script>
